Technical Q&A

HW 54 - if and then in Forth (29-Mar-99)


Q: Why does the Forth word if require a then?

A: Unlike languages such as C, Forth requires an if to be followed by an else followed by a then. In the case without an else, the then must still follow an if. Since any character, number, or symbol can be a Forth word, you cannot use a pair of parentheses to encase the code following an if or else. The left paren character, (, is a Forth word. It tells the Forth interpreter to stop interpeting input and collect all text, as comments, until a right paren character, ), is encountered. A more rigorous definition for the if word is:

The then is the delimiter to the true or false function, since the true action follows the if statement and the false action follows the else statement. Other non-conditional code can follow the “if then” clause before the end of the definition.


-- Wayne Flansburg
Worldwide Developer Technical Support

Technical Q&As | Contents
Previous Question | Next Question

To contact us, please use the Contact Us page.